package libnetdb.dm.assoc.base;

import java.util.*;

class BasicRuleIterator implements Iterator{

    private LitemsetNode root;
    private int support_lmt;
    private LatticeIterator iter;
    private RuleSet ruleset;
    private Rule rule;

    BasicRuleIterator(
            RuleSet ruleset, LitemsetNode root, double confidence){
        this.root = root;
        this.ruleset = ruleset;
        this.iter = root.decesterIterator();
        this.support_lmt = (confidence==0 ? Integer.MAX_VALUE :
                (int)Math.floor(root.getLitemset().count/confidence));
        this.rule = moveNext();
    }

    public Rule moveNext(){        
        while(iter.hasNext()){
            LitemsetNode node = (LitemsetNode) iter.next();
            if(node.getLitemset().count <= support_lmt)
                return ruleset.createRule(root, node);
            else iter.hardPrune();
        }        
        return null;
    }
    
    public boolean hasNext(){
        return rule != null;    
    }
    
    public Object next(){
        if(rule != null){
            Object current = rule;
            rule = moveNext();
            return current;
        }else return null;
    }
    
    public void remove(){
        throw new UnsupportedOperationException("remove");
    }

}